// 
// Decompiled by Procyon v0.5.36
// 

package com.orange.boot.dev2.service.impl;

import java.lang.invoke.SerializedLambda;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.orange.boot.dev3.entity.Picture;
import java.io.Serializable;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orange.boot.utils.R;
import com.orange.boot.dev2.entity.vo.CatalogueVO;
import java.util.List;
import com.orange.boot.dev2.entity.vo.DiseasePictureNameVO;
import com.orange.boot.dev2.entity.vo.ShowDiseaseInfoVO;
import com.orange.boot.dev2.entity.vo.BrowseParamsVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import com.orange.boot.dev3.mapper.PictureMapper;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.orange.boot.dev2.service.DiseaseInformationService;
import com.orange.boot.dev2.entity.DiseaseInformation;
import com.orange.boot.dev2.mapper.DiseaseInformationMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class DiseaseInformationServiceImpl extends ServiceImpl<DiseaseInformationMapper, DiseaseInformation> implements DiseaseInformationService {
    @Resource
    DiseaseInformationMapper diseaseInformationMapper;
    @Autowired
    private PictureMapper pictureMapper;

    public DiseaseInformationServiceImpl() {
    }

    public Page<BrowseParamsVO> browse(Integer pageNum, Integer pageSize) {
        return this.diseaseInformationMapper.browseParams(new Page((long)pageNum, (long)pageSize));
    }

    public Page<BrowseParamsVO> browseLike(Integer pageNum, Integer pageSize, String diseaseChineseName, String disasterTypeId, String citrusPartId) {
        return this.diseaseInformationMapper.browseLike(new Page((long)pageNum, (long)pageSize), diseaseChineseName, disasterTypeId, citrusPartId);
    }

    public ShowDiseaseInfoVO showDiseaseInformation(String diseaseId) {
        return this.diseaseInformationMapper.showDiseaseInformation(diseaseId);
    }

    public Page<DiseasePictureNameVO> showDiseasePictureName(Integer pageNum, Integer pageSize) {
        return this.diseaseInformationMapper.showDiseasePictureName(new Page((long)pageNum, (long)pageSize));
    }

    public List<ShowDiseaseInfoVO> diseaseForNameLike(String diseaseChineseName) {
        return this.diseaseInformationMapper.diseaseForNameLike(diseaseChineseName);
    }

    public Page<DiseasePictureNameVO> diseaseSelect(Integer pageNum, Integer pageSize, String diseaseChineseName, String disasterTypeId, String citrusPartId) {
        return this.diseaseInformationMapper.diseaseLike(new Page((long)pageNum, (long)pageSize), diseaseChineseName, disasterTypeId, citrusPartId);
    }

    public List<CatalogueVO> catalogue(String disasterTypeId, String citrusPartId) {
        return this.diseaseInformationMapper.catalogue(disasterTypeId, citrusPartId);
    }

    public R nameToID(String name) {
        // 创建 LambdaQueryWrapper 实例
        LambdaQueryWrapper<DiseaseInformation> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 处理名称中的特定关键词
        if (name.contains("柑橘") || name.contains("柑桔") || name.contains("叶片") || name.contains("果实")) {
            name = name.substring(2);
        }

        // 查询 DiseaseInformation 对象
        DiseaseInformationMapper diseaseInformationMapper = (DiseaseInformationMapper) this.baseMapper;
        DiseaseInformation diseaseInformation = diseaseInformationMapper.selectOne(
                lambdaQueryWrapper.select(DiseaseInformation::getDiseaseId)
                        .like(DiseaseInformation::getDiseaseChineseName, name)
        );

        // 获取 DiseaseId
        String diseaseId = diseaseInformation != null ? diseaseInformation.getDiseaseId() : null;

        // 返回结果
        return diseaseId != null ? R.ok().data("diseaseId", diseaseId) : R.error("查无此数据");
    }


    public R logicDelete(String diseaseId) {
        int i = this.diseaseInformationMapper.deleteById(diseaseId);
        return i > 0 ? R.ok() : R.error();
    }

    @Transactional
    public R addDiseaseInformation(DiseaseInformation diseaseInformation) {
        int insert = this.diseaseInformationMapper.insert(diseaseInformation);
        Picture picture = new Picture();
        picture.setDiseaseId(diseaseInformation.getDiseaseId());
        picture.setPictureName(diseaseInformation.getDiseaseChineseName());
        picture.setCitrusPartId(diseaseInformation.getCitrusPartId());
        picture.setPictureUrl(diseaseInformation.getDiseasePictureUrl());
        int insert1 = this.pictureMapper.insert(picture);
        return insert1 == insert ? R.ok() : R.error();
    }

    @Transactional
    public R updateDiseaseInformation(DiseaseInformation diseaseInformation) {
        LambdaUpdateWrapper<DiseaseInformation> updateWrapper = new LambdaUpdateWrapper();
        if (!StringUtils.hasText(diseaseInformation.getDiseaseId())) {
            throw new RuntimeException("修改信息没有传入id");
        } else {
            updateWrapper.eq(DiseaseInformation::getDiseaseId, diseaseInformation.getDiseaseId());
            updateWrapper.set(StringUtils.hasText(diseaseInformation.getDiseaseChineseName()), DiseaseInformation::getDiseaseChineseName, diseaseInformation.getDiseaseChineseName());
            updateWrapper.set(StringUtils.hasText(diseaseInformation.getDiseaseEnglishName()), DiseaseInformation::getDiseaseEnglishName, diseaseInformation.getDiseaseEnglishName());
            updateWrapper.set(StringUtils.hasText(diseaseInformation.getDiseaseTrivialName()), DiseaseInformation::getDiseaseTrivialName, diseaseInformation.getDiseaseTrivialName());
            updateWrapper.set(StringUtils.hasText(diseaseInformation.getDiseaseTreatment()), DiseaseInformation::getDiseaseTreatment, diseaseInformation.getDiseaseTreatment());
            updateWrapper.set(StringUtils.hasText(diseaseInformation.getDisasterTypeId()), DiseaseInformation::getDisasterTypeId, diseaseInformation.getDisasterTypeId());
            updateWrapper.set(StringUtils.hasText(diseaseInformation.getDiseasePathogen()), DiseaseInformation::getDiseasePathogen, diseaseInformation.getDiseasePathogen());
            updateWrapper.set(StringUtils.hasText(diseaseInformation.getDiseasePictureUrl()), DiseaseInformation::getDiseasePictureUrl, diseaseInformation.getDiseasePictureUrl());
            updateWrapper.set(StringUtils.hasText(diseaseInformation.getDiseaseIntroduce()), DiseaseInformation::getDiseaseIntroduce, diseaseInformation.getDiseaseIntroduce());
            updateWrapper.set(DiseaseInformation::getDiseaseIntelligence, 0);
            updateWrapper.set(StringUtils.hasText(diseaseInformation.getCitrusPartId()), DiseaseInformation::getCitrusPartId, diseaseInformation.getCitrusPartId());
            updateWrapper.set(DiseaseInformation::getDiseaseWeight, 80);
            updateWrapper.set(StringUtils.hasText(diseaseInformation.getDiseaseDistribution()), DiseaseInformation::getDiseaseDistribution, diseaseInformation.getDiseaseDistribution());
            updateWrapper.set(StringUtils.hasText(diseaseInformation.getDiseaseVideo()), DiseaseInformation::getDiseaseVideo, diseaseInformation.getDiseaseVideo());
            int update = this.diseaseInformationMapper.update(diseaseInformation, updateWrapper);
            return update == 1 ? R.ok() : R.error();
        }
    }
}